package com.kitty.game.zhenmolu;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.message.ReqCloseDlg;
import com.kitty.game.confirm.model.BuyZMLConfirm;
import com.kitty.game.confirm.model.FightSealBossConfirm;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.EquipField;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.fuling.FulingController;
import com.kitty.game.hunpo.service.HunPoService;
import com.kitty.game.hunqiao.HunQiaoService;
import com.kitty.game.hunqiao.message.*;
import com.kitty.game.hunqiao.model.HunQiaoStoreEquipInfo;
import com.kitty.game.hunqiao.model.HunQiaoToSave;
import com.kitty.game.hunqiao.model.HunQiaoZhuRuEquipInfo;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespConfirm;
import com.kitty.game.utils.Const;
import com.kitty.game.zhenmolu.message.*;
import com.kitty.game.zhenmolu.model.ZhenMoLu;
import com.kitty.game.zhenmolu.service.ZhenMoLuService;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.Message;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 镇魔录
 */
@Controller
@Slf4j
public class ZhenMoLuController {

   Logger logger = LoggerFactory.getLogger(ZhenMoLuController.class);

   @Autowired
   private ZhenMoLuService zhenMoLuService;

   @Autowired
   private EquipService equipService;

   @RequestMapping
   public void buyZML(IoSession session, ReqZhenMoLuFetch reqZhenMoLuFetch){
      Role role = SessionUtils.getRoleBySession(session);


//        //结果 MSG_ZHENMOLU_RESULT             = 45914;
//        RespZhenMoLuResult respZhenMoLuResult = new RespZhenMoLuResult();
//        respZhenMoLuResult.setFlag((byte) 1);
//        respZhenMoLuResult.setFmLevel((byte) role.getZmLevel());
//        respZhenMoLuResult.setYmType((byte) 0);
//        respZhenMoLuResult.setItemRate((byte) 1);
//        MessagePusher.pushMessage(role, respZhenMoLuResult);

      //MSG_ZHENMOLU_BONUS              = 45915;
      String itemName = "";
      byte count = 0;
      byte itemRate = 0;

      if(role.getYmType() == 1){
         itemName = "大妖内丹";
         count = 2;
      }
      else if(role.getYmType() == 2){
         itemName = "妖王内丹";
         count = 1;
      }
      if(role.getZmLevel() ==1){
         itemRate = 1;
      }
      else if(role.getZmLevel() ==2){
         itemRate = 5;
      }
      else if(role.getZmLevel() ==3){
         itemRate = 5;
      }
      RespZhenMoLuBonus respZhenMoLuBonus = new RespZhenMoLuBonus();
      respZhenMoLuBonus.setItemName(itemName);
      respZhenMoLuBonus.setCount((byte) (count * itemRate));
      respZhenMoLuBonus.setItemRate(itemRate);
      MessagePusher.pushMessage(role, respZhenMoLuBonus);

      if(itemName.contains("丹")){
         SpringUtils.getChatService().sendNumor("恭喜#Y"+role.getName()+"#n运气爆棚，在镇魔录中获得了#R"+count * itemRate+"#n个"+itemName, Const.BRODCAST_MSG_TYPE_ROLE);
         equipService.addMall(itemName,true,role,count * itemRate);
      }

   }
   /**
    * 购买镇魔令
    * @param session
    * @param respZhenMoLuBuy
    */
   @RequestMapping
   public void buyZML(IoSession session, RespZhenMoLuBuy respZhenMoLuBuy){
      Role role = SessionUtils.getRoleBySession(session);
      int price = 3000000;//300w文
      int count = (int) respZhenMoLuBuy.getCount();


      role.setConfirm(new BuyZMLConfirm(count));

      RespConfirm respConfirm = new RespConfirm();
      respConfirm.setTips("确认花费#R"+price*count+"文钱#n，兑换"+count+"枚#R镇魔令#n？\n（兑换成镇魔令后将无法直接把镇魔令再兑换成金钱，镇魔令只能用于参与镇魔录系统）");
      MessagePusher.pushMessage(role, respConfirm);

   }

   @RequestMapping
   public void zhenMoStart(IoSession session, ReqZhenMoLuStart reqZhenMoLuStart){
      Role role = SessionUtils.getRoleBySession(session);
      int level = reqZhenMoLuStart.getLevel();
      int total = 1;

      if(level == 1){
         total = level;
      }
      else if(level == 2){
         total = level * 2;
      }
      else if(level == 3){
         total = level * 3;
      }
      zhenMoLuService.zhenmoStart(role, total, reqZhenMoLuStart.getLevel());

   }

   /**
    * 关闭对话框
    * @param session
    * @param reqCloseDlg
    */
   @RequestMapping
   public void closeZml(IoSession session, ReqCloseDlg reqCloseDlg){
      Role role = SessionUtils.getRoleBySession(session);
      String param1 = reqCloseDlg.getPara1();

      if(param1.equals("zhenmolu")){//关闭镇魔录
         logger.debug("关闭zhenmolu....");

         RespZhenMoLuClose respZhenMoLuClose = new RespZhenMoLuClose();
         MessagePusher.pushMessage(role, respZhenMoLuClose);
      }

   }



   @RequestMapping
   public void ReqZhenmoluChange(IoSession session, ReqZhenmoluChange reqZhenmoluChange) {
      Role role = SessionUtils.getRoleBySession(session);
      switch (reqZhenmoluChange.getType()){
         case 0:
            if (reqZhenmoluChange.getItemName() != null) {
               switch (reqZhenmoluChange.getItemName()) {
                  case "大妖内丹":
                     int countDy = equipService.getAllCount(reqZhenmoluChange.getItemName(), role);
                     if (countDy <= 0) {
                        MessagePusher.pushMessage(role, new RespNotifyMiscEx("你身上没有大妖内丹！！！"));
                        break;
                     }
                     equipService.delRoloEquip("大妖内丹", 1, false, role);
                     SpringUtils.getRoleService().addMoney(role, 1 * 6000000);
                     break;
                  case "妖王内丹":
                     int countYw = equipService.getAllCount(reqZhenmoluChange.getItemName(), role);
                     if (countYw <= 0) {
                        MessagePusher.pushMessage(role, new RespNotifyMiscEx("你身上没有妖王内丹！！！"));
                        break;
                     }
                     equipService.delRoloEquip("妖王内丹", 1, false, role);
                     SpringUtils.getRoleService().addMoney(role, 1 * 36000000);
                     break;
               }
            } else {
               MessagePusher.pushMessage(role, new RespNotifyMiscEx("没有内丹不能提交！！！"));
            }
            break;
         case 1:
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("#Y一键提交暂未开放，请点击提交按钮！！！"));
//            int countDy = equipService.getAllCount("大妖内丹", role);
//            int countYw = equipService.getAllCount("妖王内丹", role);
//            if (countDy <= 0 && countYw <= 0) {
//               MessagePusher.pushMessage(role, new RespNotifyMiscEx("你身上没有内丹！！！"));
//               break;
//            }
//            if (countDy > 0) {
//               delNeiDan(role, "大妖内丹");
//            }
//            if (countYw > 0) {
//               delNeiDan(role, "妖王内丹");
//            }
//            SpringUtils.getRoleService().addMoney(role, countDy * 6000000+countYw * 36000000);
//            break;
      }
      MessagePusher.pushMessage(role, reqZhenmoluChange);

   }

   public void delNeiDan(Role role, String name) {
      for (RoleEquip roleEquip : role.getEquipBox().getEquips().values()) {
         if (roleEquip == null) {
            continue;
         }
         if (roleEquip.getPosition() < 41 | roleEquip.getPosition() >= 166) {
            continue;
         }
         if (roleEquip.getName().equals(name)) {
            equipService.delRoleEquip(roleEquip, role);
         }
      }
   }
}
